package top.frsf.envi;

import top.frsf.GameObject;
import top.frsf.GamePanel;
import top.frsf.utils.GameState;

import java.awt.*;

/**
 * @BelongsProject: MiniPZ
 * @BelongsPackage: top.frsf.envi
 * @Author: FRSF
 * @CreateTime: 2022-09-06  14:39
 * @Description: 游戏背景加载
 * @Version: 1.0
 */
public class Background extends GameObject {

    boolean rightMove = false;
    boolean leftMove = false;

    public Background(String img, int x, int y, int width, int height) {
        super(img, x, y, width, height);
    }


    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        if (GamePanel.stage == GameState.START)
            move();
    }


    @Override
    public Rectangle getRec() {
        return super.getRec();
    }

    private void move() {

        if (rightMove && leftMove) {
            GamePanel.stage = GameState.RUNNING;
        } else if (!rightMove) {
            // 开局右移
            if (Math.abs(getX()) + GamePanel.WIDTH < getWidth()) {
                setX(getX() - 1);
            } else rightMove = true;
        } else {
            if (getX() < -175) {
                setX(getX() + 1);
            } else {
                leftMove = true;
            }
        }
    }
}
